package com.jingwu.example.graphql;

import com.coxautodev.graphql.tools.GraphQLMutationResolver;
import com.jingwu.example.pojo.dto.AuthorDTO;
import com.jingwu.example.pojo.dto.BookDTO;
import com.jingwu.example.pojo.dto.CategoryDTO;
import com.jingwu.example.pojo.entity.AuthorDO;
import com.jingwu.example.pojo.entity.CategoryDO;
import com.jingwu.example.pojo.vo.BookVO;
import com.jingwu.example.repo.AuthorRepository;
import com.jingwu.example.repo.CategoryRepository;
import org.springframework.stereotype.Service;

import static com.deepexi.web.support.pojo.converter.utils.ConverterUtils.convert;

/**
 * @author jingwu
 * @since 2021/2/3
 */

@Service
public class AuthorMutationResolver implements GraphQLMutationResolver {

    private final AuthorRepository repository;

    public AuthorMutationResolver(AuthorRepository repository) {
        this.repository = repository;
    }

    public Integer saveAuthor(AuthorDTO dto) {
        AuthorDO entity = convert(dto, AuthorDO.class);
        repository.insert(entity);
        return entity.getId();
    }

    public Boolean updateAuthor(Integer id, AuthorDTO dto) {
        AuthorDO entity = convert(dto, AuthorDO.class);
        entity.setId(id);
        return repository.update(entity);
    }

}
